/**
 * @lastUpdateBy : ouyyc
 * @description: bim借口相关api
 */
class bimDataApi{
	constructor(){
		this.originBIMServerUrl = agcim.net.apiPath._agcim_server_bim;
	}

	/**
	 * @description: 恢复bim接口为默认服务器
	 * @return {*}
	 */
	resetBIMServer(){
		agcim.net.apiPath.setAgcimServerBIM(this.originBIMServerUrl);
	}

	/**
	 * @description: 根据服务url切换bim接口服务器
	 * @param {*} url
	 * @return {*}
	 */
	setBIMServerByLayerUrl(url){
		if(!url) return
		let index = url.indexOf("/",8);
		let bimServerUrl = url;
		if(index > 0){
			bimServerUrl = url.slice(0,index);
		}
		bimServerUrl += "/bim-rest";
		agcim.net.apiPath.setAgcimServerBIM(bimServerUrl);
	}

	/**
	 * @description: 根据服务url获取模型相关信息，比如buildingid【bldgId】、平面图【agSrvContentDraws】等
	 * @param {*} url
	 * @return {*}
	 */
	getBIMInfoByLayerUrl(url){
		if(!url) return Promise.reject();
		let newUrl = url.replace("preview","service");
		newUrl = newUrl.replace("/tileset.json","");
		newUrl += "?format=json";
		return new Promise((resolve, reject) => {
			agcim.net.axiosWraper.getData(newUrl).then(
				(response) => {
					if (response) {
						resolve(response);
					} else {
						reject(response);
					}
				},
				(error) => {
					reject(error);
				}
			)
		})
	}
	/**
	 * @description: 获取数据
	 * @param {Object} params 参数
	 * @return {Promise}
	 */
	find3dentity(params,url) {
		if(url) this.setBIMServerByLayerUrl(url);
		return new Promise((resolve, reject) => {
			agcim.net.axiosWraper
				.getData(
					agcim.net.apiPath.bimParamethy_dentity_listEntity,
					params
				)
				.then(
					(response) => {
						if (response.success) {
							resolve(response);
						} else {
							reject(response);
						}
					},
					(error) => {
						reject(error);
					}
				);
		});
	}

	/**
	 * @description: 获取材质
	 * @param {Object} params
	 * @return {Promise}
	 */
	getMaterial(params,url) {
		if(url) this.setBIMServerByLayerUrl(url);
		return new Promise((resolve, reject) => {
			agcim.net.axiosWraper
				.getData(
					agcim.net.apiPath.bimParamethy_dentity_listMaterial,
					params
				)
				.then(
					(response) => {
						if (response.success) {
							resolve(response);
						} else {
							reject(response);
						}
					},
					(error) => {
						reject(error);
					}
				);
		});
	}

	/**
	* @description: 获取建筑表信息
	* @param {Object} params 
	* @returns {Promise}
	*/
	getBuilding(params,url) {
		if(url) this.setBIMServerByLayerUrl(url);
		return new Promise((resolve, reject) => {
			agcim.net.axiosWraper
				.getData(agcim.net.apiPath.bimParamethy_building_list, params)
				.then(
					(response) => {
						if (response.success) {
							resolve(response);
						} else {
							reject(response);
						}
					},
					(error) => {
						reject(error);
					}
				);
		});
	}

	/**
	* @description: 根据项目id获取建筑列表
	* @param {Object} params 
	* @returns {Promise}
	*/
	getBuildingByProjectId(params,url){
		if(url) this.setBIMServerByLayerUrl(url);
		return new Promise((resolve, reject) => {
			agcim.net.axiosWraper
				.getData(agcim.net.apiPath.bimParamethy_building_get, params)
				.then(
					(response) => {
						if (response.success) {
							resolve(response);
						} else {
							reject(response);
						}
					},
					(error) => {
						reject(error);
					}
				);
		});
	}

	/**
	 * @description: 获取图纸
	 * @param {Object} params
	 * @return {Promise}
	 */
	findDraw(params,url) {
		if(url) this.setBIMServerByLayerUrl(url);
		return new Promise((resolve, reject) => {
			agcim.net.axiosWraper
				.getData(
					agcim.net.apiPath.bimDrawing_findDraw,
					params
				)
				.then(
					(response) => {
						if (response.success) {
							resolve(response);
						} else {
							reject(response);
						}
					},
					(error) => {
						reject(error);
					}
				);
		});
	}

	/**
	 * @description: 获取项目表
	 * @param {*} params
	 * @return {*}
	 */
	findProject(params,url) {
		if(url) this.setBIMServerByLayerUrl(url);
		return new Promise((resolve, reject) => {
			agcim.net.axiosWraper
				.getData(
					agcim.net.apiPath.bimParamethy_project,
					params
				)
				.then(
					(response) => {
						if (response.success) {
							resolve(response);
						} else {
							reject(response);
						}
					},
					(error) => {
						reject(error);
					}
				);
		});
	}

	/**
	 * @description: 寻找最短路径
	 * @param {*} params
	 * @param {*} url
	 * @return {*}
	 */
	findDistance(params,url) {
		if(url) this.setBIMServerByLayerUrl(url);
		return new Promise((resolve, reject) => {
			agcim.net.axiosWraper
				.getData(
					agcim.net.apiPath.bimDrawing_distance,
					params
				)
				.then(
					(response) => {
						if (response.success) {
							resolve(response);
						} else {
							reject(response);
						}
					},
					(error) => {
						reject(error);
					}
				);
		});
	}
	/**
	 *
	 *
	 * 获取表名
	 * @param {*} projectId 要查询模型id
	 * @return {*} data{success：,content: }
	 */
	async getTableName (projectId, url) {
		if (url) this.setBIMServerByLayerUrl(url);
		return new Promise((resolve, reject) => {
			agcim.net.axiosWraper
				.getData(
					`${agcim.net.apiPath.bimParamethy_building_get}?paramType=1&bldgId=${projectId}`,
				)
				.then(
					(response) => {
						if (response.success) {
							resolve(response);
						} else {
							reject(response);
						}
					},
					(error) => {
						reject(error);
					}
				);
		});
	}
}

export default new bimDataApi()
